iT邦幫忙

2024 iThome 鐵人賽

DAY 6
1
Software Development

Laravel 隨筆學習札記系列 第 6

Day6 - 知道 Eloquent ORM 魔術師的隱藏技能嗎?

  • 分享至 

  • xImage
  •  

昨天提到 Eloquent ORM 能連結不同的玩具,今天就來探索 Eloquent ORM 的隱藏技能吧!準備好了嗎?讓我們開始吧🤓

管理玩具小夥伴

魔術師 Eloquent ORM 的玩具箱裡不只管理單一玩具,有時候玩具們會有自己的小夥伴、小家族。
像是一對一hasOne、一對多hasMany或者多對多belongsToMany的玩具關聯。我們一起來看看怎麼管理這些玩具小夥伴~

1. 一對一的玩具關聯:hasOne

一輛玩具車 (Car) 跟一台遙控器 (RemoteControl)是一組的好夥伴

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;

// 定義一對一的關聯
class Car extends Model
{
    public function RemoteControl(): HasOne
    {
        return $this->hasOne(RemoteControl::class);
    }
}

// 定義反向關聯
class RemoteControl extends Model
{
    public function car(): BelongsTo
    {
        return $this->belongsTo(Car::class);
    }
}

呼喚我的玩具車、遙控器:

// [從玩具車呼喚遙控器]
$car = Car::find(1); // 找到 ID 為 1 的遙控車
$remote = $car->remote; // 獲取對應的遙控器

// [從遙控器呼喚玩具車]
$RemoteControl = RemoteControl::find(1); // 找到 ID 為 1 的遙控器
$car = $RemoteControl->car; // 獲取對應的玩具車

https://ithelp.ithome.com.tw/upload/images/20240920/20169308kbHNSEgXs1.png

2. 一對多的玩具關聯:hasMany

一個公仔(Doll)可以有多個配件(Accessories)唷!

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

// 定義一對多的關聯
class Figure extends Model
{
    public function accessories(): HasMany
    {
        return $this->hasMany(Accessory::class);
    }
}

// 定義反向關聯
class Accessory extends Model
{
    public function figure(): BelongsTo
    {
        return $this->belongsTo(Figure::class);
    }
}

使用方式:

// [從公仔呼喚多個配件]
$figure = Figure::find(1); // 找到 ID 為 1 的公仔
$accessories = $figure->accessories; // 獲取對應的所有配件

// [從其中一個配件呼喚對應公仔]
$accessory = Accessory::find(1); // 找到 ID 為 1 的配件
$figure = $accessory->figure; // 獲取對應的公仔

https://ithelp.ithome.com.tw/upload/images/20240920/20169308RL7GK9mMmq.png

3. 多對多的玩具關聯:belongsToMany

不同花樣的卡牌(Card)和多種玩法(Gameplay

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

// 定義多對多的關聯
class Card extends Model
{
    public function gameplays(): BelongsToMany
    {
        return $this->belongsToMany(Gameplay::class);
    }
}

// 定義多對多的關聯
class Gameplay extends Model
{
    public function cards(): BelongsToMany
    {
        return $this->belongsToMany(Card::class);
    }
}

使用方式:

// [從其中一種玩法中找到對應的卡牌]
$gameplay = Gameplay::find(1); // 找到 ID 為 1 的玩法
$cards = $gameplay->cards; // 獲取對應的所有卡牌

// [從其中一組卡牌中找到對應的玩法]
$card = Card::find(1); // 找到 ID 為 1 的卡牌
$gameplays = $card->gameplays; // 獲取對應的所有玩法

https://ithelp.ithome.com.tw/upload/images/20240920/20169308WscLjeS9KS.png


Eloquent ORM 的其他魔法功能

1. 查詢玩具的魔法

我們可以用小魔法來過濾我們的玩具,比如只找出所有的「新玩具」。

$newToys = Toy::where('status', 'new')->get();

2. 玩具的魔法

我們可以設定一些規則,比如在每次創造新玩具時自動貼上狀態標籤。

class Toy extends Model
{
    protected static function booted()
    {
        static::creating(function ($toy) {
            $toy->status = 'new';
        });
    }
}

參考資料

  1. 一對一
  2. 一對多
  3. 多對多關係

踏著身心靈的塔羅腳步,轉向技術與邏輯的工程師之路,就藉由塔羅日抽來紀錄今日的學習與生活吧!
金幣侍者:目前都還在學習的階段,保持持續前進的動力才能有好的收穫唷!

It does not matter how slowly you go so long as you do not stop.
走得多慢都無所謂,只要妳不停下腳步。
--- Andy Warhol 安迪‧沃荷


上一篇
Day5 - Eloquent ORM 是魔法師?
下一篇
Day7 - 認識查詢小幫手:Laravel 的 Query Scopes
系列文
Laravel 隨筆學習札記13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言